package com.gensdai.leliang.dialog;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flyco.roundview.RoundTextView;
import com.gensdai.leliang.R;
import com.gensdai.leliang.activity.OrderPreperActivity;
import com.gensdai.leliang.activity.forget_mm;
import com.gensdai.leliang.activity.set_pay_mm;
import com.gensdai.leliang.activity.user_enter;
import com.gensdai.leliang.popupwindow.BasePopupWindow;
import com.gensdai.leliang.utils.BaseUtils;
import com.gensdai.leliang.utils.PreferencesUtils;
import com.mock.alipay.Callback;
import com.mock.alipay.PasswordKeypad;

/**
 * Created by Administrator on 2017/6/7.
 */

public class dialog_select_pay_type_test extends BasePopupWindow {


    AppCompatCheckBox checkali;
    RelativeLayout alipaylayout;

    AppCompatCheckBox checkyue;
    RelativeLayout yuepaylayout;

    AppCompatCheckBox checkwechat;
    RelativeLayout wechatlayout;


    private View popupView;
    private RoundTextView paybtn;
    private TextView pricelable;
    ImageView closeBtn;                    //关闭按钮

    private String priceText = "0";

    dialog_setup_password pass;
    public PasswordKeypad keypad;
    private boolean hasPayPassword = false;
    private String userid = "";   /*000000*/
    KeybordListener keybordListener;

    PayTypeClick click;

    FragmentManager fragmentManager;

    private String orderID = null;

    int payType = 1;  //默认0是app余额支付  1为支付宝  2是微信


    public void setOrderID(String orderID) {
        this.orderID = orderID;
    }

    public void setPrice(float priceText) {
        this.priceText = BaseUtils.formatFloat(priceText);
        if (pricelable != null) {
            pricelable.setText("¥" + BaseUtils.formatFloat(priceText));
        }
       float p = priceText;
        if(p <= 0){
            //隐藏掉支付宝支付
            if(alipaylayout != null)
                alipaylayout.setVisibility(View.GONE);
            if(wechatlayout != null)
                wechatlayout.setVisibility(View.GONE);


            payType = 0;
            changeRadioButton();
        }
    }


    public void setOnPayTypeClick(PayTypeClick click) {
        this.click = click;
    }

    public void setOnKeyBordCallBackListener(KeybordListener keybordListener) {
        this.keybordListener = keybordListener;
    }

    //设置keypad的信息
    public void setkeyPadStatus(boolean flag) {
        if (keypad != null) {
            keypad.setPasswordState(flag);
        }
    }

    public void setkeyPadStatus(boolean flag, String message) {
        if (keypad != null) {
            keypad.setPasswordState(flag, message);
        }
    }

    public dialog_select_pay_type_test(Activity context, FragmentManager fragmentManager) {
        super(context);
        this.fragmentManager = fragmentManager;
        bindview();
    }

    private void bindview() {

        hasPayPassword = PreferencesUtils.getBooleanPreference(getContext(), OrderPreperActivity.prefreceName, OrderPreperActivity.prefreceKey, false); //获取是否设置过支付密码
        userid = PreferencesUtils.getStringPreference(getContext(), OrderPreperActivity.prefreceName, OrderPreperActivity.prefrece_userid, ""); //获取用户id没有登陆就是没有


        pricelable = (TextView) popupView.findViewById(R.id.priceLable);
        paybtn = (RoundTextView) popupView.findViewById(R.id.paybtn);
        closeBtn = (ImageView) popupView.findViewById(R.id.closeBtn);

        checkali = (AppCompatCheckBox) popupView.findViewById(R.id.checkali);
        alipaylayout = (RelativeLayout) popupView.findViewById(R.id.alipaylayout);
        checkyue = (AppCompatCheckBox) popupView.findViewById(R.id.checkyue);
        yuepaylayout = (RelativeLayout) popupView.findViewById(R.id.yuepaylayout);
        checkwechat = (AppCompatCheckBox) popupView.findViewById(R.id.checkwechat);
        wechatlayout = (RelativeLayout) popupView.findViewById(R.id.wechatlayout);

        changeRadioButton();
        checkali.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payType = 1;
                changeRadioButton();
            }
        });
        alipaylayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payType = 1;
                changeRadioButton();
            }
        });
        checkwechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payType = 2;
                changeRadioButton();
            }
        });
        wechatlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payType = 2;
                changeRadioButton();
            }
        });


        yuepaylayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payType = 0;
                changeRadioButton();
            }
        });
        checkyue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                payType = 0;
                changeRadioButton();
            }
        });


        if (keypad == null) {
            keypad = new PasswordKeypad();
            keypad.setCancelable(false);
            keypad.setPasswordCount(6);
            keypad.setCallback(new Callback() {
                @Override
                public void onForgetPassword() {
                    Toast.makeText(getContext(), "忘记密码", Toast.LENGTH_LONG).show();
                    if (keybordListener != null) {
                        if(getContext().getSharedPreferences("User",getContext().MODE_PRIVATE).getBoolean("enter_state",false)){
                            Intent go_forget_mm = new Intent(getContext(), forget_mm.class);
                            go_forget_mm.putExtra("title",getContext().getString(R.string.find_pay_mm));
                            getContext().startActivity(go_forget_mm);
                        }else{
                            getContext().startActivity(new Intent(getContext(),user_enter.class));
                        }

                        keybordListener.onForgetPassword();
                    }
                }

                @Override
                public void onInputCompleted(CharSequence password) {

                    if (keybordListener != null) {
                        if (orderID != null) {
                            keybordListener.onInputCompleted(password, orderID);
                        } else {

                            keybordListener.onInputCompleted(password);
                        }
                    }
                }

                @Override
                public void onPasswordCorrectly() {
                    keypad.dismiss();

                    if (keybordListener != null) {
                        keybordListener.onPasswordCorrectly();
                    }
                }

                @Override
                public void onCancel() {
                    if (keybordListener != null) {
                        keybordListener.onCancel();
                    }
                }
            });
        }


        paybtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断是否选择支付宝支付 默认余额支付
                payBtnClickListener();
            }
        });

        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void changeRadioButton(){
        if(payType == 0){
            checkyue.setChecked(true);
            checkali.setChecked(false);
            checkwechat.setChecked(false);
        }else if(payType == 1){
            checkyue.setChecked(false);
            checkali.setChecked(true);
            checkwechat.setChecked(false);
        }else if(payType == 2){
            checkyue.setChecked(false);
            checkali.setChecked(false);
            checkwechat.setChecked(true);
        }
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_select_pay_type, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return popupView.findViewById(R.id.chidView);
    }

    @Override
    protected Animation initShowAnimation() {
        return getTranslateAnimation(250 * 5, 0, 260);
    }

    @Override
    protected Animation initExitAnimation() {
        return getTranslateAnimation(0, 250 * 5, 260);
    }

    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }


    private void payBtnClickListener() {
        //判断用户是否登录
        if (!TextUtils.isEmpty(userid)) {
            if (payType == 1) {
                if (click != null) {
                    //支付宝支付
                    click.PayAli(orderID);
                }
            }else if(payType == 2){
                if (click != null) {
                    //微信支付
                    click.PayWechat(orderID);
                }
            }else {
                if (hasPayPassword) {
//                keypad.show(getSupportFragmentManager(), "passwordKeyPad");
//                payTypeDialog.showPopupWindow();
//                    postOrder();
                    keypad.show(fragmentManager, "passwordKeyPad");
                    //显示余额支付的窗口

                } else {
                    pass = new dialog_setup_password(getContextActivity());
//                pass.setOnMenuClick(menuClick);
                    pass.showPopupWindow();
                }
            }

        } else {
            Toast.makeText(getContext(), "用户ID为空", Toast.LENGTH_SHORT).show();
        }
    }

    /*interface*/

    public interface KeybordListener {
        void onInputCompleted(CharSequence password);

        void onInputCompleted(CharSequence password, String ordereid);

        void onForgetPassword();

        void onPasswordCorrectly();  //密码输入完成后的处理

        void onCancel();

    }

    public interface PayTypeClick {
        void PayAli(String orderID);
        void PayWechat(String orderID);
    }
}
